/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Gui;

import DataModel.*;
import Utilities.EnumConverter;
import java.text.*;
import java.util.Date;

/**
 *
 * @author Rainiero
 */
public class GuiHelper
{

    // <editor-fold defaultstate="collapsed" desc="Singleton">
    private static GuiHelper _instance = new GuiHelper();

    private GuiHelper()
    {
    }

    public static GuiHelper GetInstance()
    {
        return _instance;
    }
    // </editor-fold>

    public String GetHeader()
    {
        return
                "<div class=\"page\">" +
                    "<div class=\"header\" align=\"left\">" +
                        //"<div class=\"title\">" +
                            "<h1 align=\"left\">Agencia Mundial para el Desarrollo</h1>" +
                        //"</div>" +
                    "</div>" +
                    "<div class=\"clear hideSkiplink\">" +
                "</div>"+
                "<div class=\"main\">";
    }

    public String GetFooter()
    {
        return
                "</div>" +
                "<div class=\"clear\" />" +
                "<div class=\"footer\" />";
    }

    public String GetPagination(int itemsNumber, int pageNumber, String address, int colCount)
    {
        return GetPagination(itemsNumber, pageNumber, address, colCount, 10);
    }

    public String GetPagination(int itemsNumber, int pageNumber, String address, int colCount, int displayItems)
    {
        if(itemsNumber == 0)
            return "";
        int numPages = (int)Math.ceil((double)itemsNumber / (double)displayItems);

        int startPage = Math.max(1, pageNumber - 10);
        int endPage = Math.min(pageNumber + 10, numPages);
        String result = String.format("<tr><td align=\"center\" colspan=\"%1$s\">", colCount);

        String link = String.format("<a href=\"%1$s", address);
        if(!address.contains("?"))
            link += "?";
        else if (!address.endsWith("?"))
            link += "&";
        link += "pageNum=%1$s\">%2$s</a> ";

        if(pageNumber > 1)
            result += result += String.format(link, pageNumber - 1, "<  ");

        for(int pageIndex = startPage; pageIndex <= endPage; pageIndex++)
            if(pageIndex != pageNumber)
                result += String.format(link, pageIndex, pageIndex);
            else
                result += String.format("%1$s ", pageIndex, pageIndex);


        if(pageNumber < endPage)
            result += String.format(link, pageNumber + 1, " >");
        result += String.format("<br />%1$s - %2$s de %3$s", (pageNumber - 1) * displayItems + 1, Math.min(pageNumber * displayItems, itemsNumber), itemsNumber);
        result += "</td></tr>";
        return result;
    }

    public String GetCountryOptions(Country[] countries, int selectedValue)
    {
        String result = "<option value=\"-1\">Todos los países</option>";
        for(int countryIndex = 0; countryIndex < countries.length; countryIndex++)
        {
            Country country = countries[countryIndex];
            if(country.getId() != selectedValue)
                result += String.format("<option value=\"%1$s\">%2$s</option>", country.getId(), country.getName());
            else
                result += String.format("<option value=\"%1$s\" selected=\"true\">%2$s</option>", country.getId(), country.getName());
        }
        return result;
    }

    public String GetPeopleOptions(PersonSummary[] people, int selectedValue)
    {
        String result = "";
        for(int personIndex = 0; personIndex < people.length; personIndex++)
        {
            PersonSummary person = people[personIndex];
            if(person.getId() != selectedValue)
                result += String.format("<option value=\"%1$s\">%2$s</option>", person.getId(), GetPersonText(person));
            else
                result += String.format("<option value=\"%1$s\" selected=\"true\">%2$s</option>", person.getId(), GetPersonText(person));
        }
        return result;
    }

    public String GetOrganizationLink(OrganizationSummary organization)
    {
        return String.format("<a href=\"/SAFP-war/jsps/Common/ShowOrganization.jsp?id=%1$s\">%2$s</a>", organization.getId(), organization.getName());
    }

    public String GetFormattedDate(Date date)
    {
        DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
        return formatter.format(date);
    }

    public String GetPersonText(PersonSummary person)
    {
        if(person.getName() != null)
            return String.format("%1$s %2$s %3$s", person.getName(), person.getFirstLastName(), person.getSecondLastName());
        else
            return "No asignado";
    }

    public String GetCurrencyText(double amount)
    {
        return String.format("$%1$s", amount);
    }

    public String GetPercentageText(double amount)
    {
        return String.format("%1$s%%", amount);
    }

    public String GetProjectStatus(Enums.ProjectState status)
    {
        return EnumConverter.GetEnumName(status);
    }

}
